package com.tgy.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @Author: tgy
 * @Date: 2020-08-28 11:25
 */
@Slf4j
public class InterruptThread {

    private Thread thread;

    InterruptThread(){

        thread = new Thread(()->{

            while (true){

                if (Thread.currentThread().isInterrupted()){

                    return;
                }

                log.info("run.....");

                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    //睡眠中，要重新设置一下线程的打断状态
                    Thread.currentThread().interrupt();
                }
            }


        },"interruptThread");

    }

    public void start(){

        thread.start();
    }

    public void stop(){

        thread.interrupt();
    }
}
